# -*- coding: utf-8 -*-
# @File: backend => room_api.py
# @CreateTime: 2025/8/26 19:35
# @Author: EvanZhang
# @Info: 房间相关API接口的实现

from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import JSONResponse
from typing import List

from apps.crud.room_view import create_room, join_room, get_room_snapshot, leave_room   # , exit_room
from apps.schemas.room_schema import RoomBase, RoomCreateRequest, RoomJoinRequest, RoomJoinResponse, RoomSnapshotResponse, RoomLeaveResponse
from apps.schemas.user_schema import UserBase
from apps.utils.jwt import verify_jwt_token

rooms = APIRouter(prefix='/room', tags=['房间相关'])

# @rooms.get('/list', summary='获取房间列表', response_model=List[RoomBase])
# async def get_room_list():
# 先创建房间
@rooms.post('/create', summary='创建房间', response_model=RoomBase, dependencies=[Depends(verify_jwt_token)])
async def create(room: RoomCreateRequest):
    result = await create_room(room)
    if result[0] == 201:
        return result[1]
    else:
        raise HTTPException(status_code=result[0], detail='创建房间失败:' + str(result[1]))

# 加入房间
@rooms.post('/{room_id}/join', summary='加入房间', response_model=RoomJoinResponse)
async def join(room_id: int, request: RoomJoinRequest, current_user:UserBase = Depends(verify_jwt_token)):
    # 从JWT token中获取用户ID
    user_id = current_user.id
    
    # 使用请求中的room_id（路径参数优先）
    result = await join_room(room_id, user_id, request.buy_in_amount)
    
    if result[0] == 200:
        return RoomJoinResponse(**result[1])
    else:
        raise HTTPException(status_code=result[0], detail='加入房间失败: ' + str(result[1]))

# 获取房间快照
@rooms.get('/{room_id}', summary='获取房间快照', response_model=RoomSnapshotResponse)
async def get_room(room_id: int):
    result = await get_room_snapshot(room_id)
    
    if result[0] == 200:
        return RoomSnapshotResponse(**result[1])
    else:
        raise HTTPException(status_code=result[0], detail='获取房间信息失败: ' + str(result[1]))

# 离开房间（返回简化信息版）
@rooms.post('/{room_id}/leave', summary='离开房间', response_model=RoomLeaveResponse)
async def leave(room_id: int, current_user: UserBase = Depends(verify_jwt_token)):
    # 从JWT token中获取用户ID
    user_id = current_user.id
    
    result = await leave_room(room_id, user_id)
    
    if result[0] == 200:
        return RoomLeaveResponse(msg=result[1])
    else:
        raise HTTPException(status_code=result[0], detail=result[1])

# # 退出房间（返回房间信息版）
# @rooms.post('/exit', summary='退出房间', response_model=RoomBase)
# async def exit(room_id: int, current_user: UserBase = Depends(verify_jwt_token)):
#     # 从JWT token中获取用户ID
#     user_id = current_user.id
    
#     result = await exit_room(room_id, user_id)
#     if result[0] == 200:
#         return result[1]
#     else:
#         raise HTTPException(status_code=result[0], detail='退出房间失败:' + str(result[1]))

# 开始游戏 - TODO: 待实现
# @rooms.post('/{room_id}/start', summary='开始游戏', response_model=RoomBase)
# async def start(room_id: int, current_user: UserBase = Depends(verify_jwt_token)):
#     result = await start_game(room_id)
#     if result[0] == 200:
#         return result[1]
#     else:
#         raise HTTPException(status_code=result[0], detail='开始游戏失败:' + str(result[1]))




